/*
 * @lc app=leetcode.cn id=289 lang=typescript
 *
 * [289] 生命游戏
 */

// @lc code=start
/**
 Do not return anything, modify board in-place instead.
 */
// n < 2; 1 => 0
// n = 2, 3; 1 => 1
// n > 3; 1 => 0
// n = 3; 0 => 1
// GPU 矩阵计算
// CPU 多线程
// CPU 单线程 执行时间复杂度 O(M * N)
function gameOfLife(board: number[][]): void {
  const M = board.length
  const N = board[0]?.length as number
  const next: number[][] = []
  // 九宫格偏移
  const offset = [
    [-1, -1],
    [-1, 0],
    [-1, 1],
    [0, 1],
    [1, 1],
    [1, 0],
    [1, -1],
    [0, -1],
  ]
  let val = 0
  const isOutOfRange = (i: number, j: number) => i < 0 || i >= M || j < 0 || j >= N

  const setVal = (i: number, j: number) => {
    // 获取中心值
    val = board[i]?.[j] as number
    // 1 2~3
    // 0 3
    if (val) {
      val = offset.reduce((acc: number, curr) => {
        let [i0, j0] = curr

        acc += board[i + curr[0]][j + curr[1]]
        return acc
      })
    } else {
    }
  }
  for (let i = 0; i < M; i++) {
    next.push([])
    for (let j = 0; j < N; j++) {
      setVal(i, j)
      next[i]?.push(val)
    }
  }
  board = next
}
// @lc code=end
